<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;

class Tag extends Model
{
   protected  $fillable = [
       'tag','title','subtitle','page_image','meta_description','reverse_direction'
   ];
    /*
     * 定义文章与标签之间多对多关联关系
     */

    public function posts(){
        return $this->belongsTomMany(Post::class,'post_tag_pivot');

    }
    /*
     * 从列表添加一些标签
     */
    public static function addNeededTags(array $tags){

          if(count($tags) === 0){
                    return;
           }
        $found = static::whereIn('tag',$tags)->get()->pluck('tag')->all();

        foreach(array_diff($tags,$found) as $tag){
            static::create([
                'tag'                => $tag,
                'title'              => $tag,
                'subtitle'           => 'Subtitle for'.$tag,
                'page_image'         => '',
                'meta_description'   => '',
                'reverse_direction'  => false,
            ]);
        }
    }
}
